﻿/*
=======================================================
    Stock【库存信息】
=======================================================
*/

using System.Collections;
using System.Collections.Generic;
using Entities;

namespace Services
{
    public interface IStockLogic : ILogic<StockModel, StockFilterModel>
    {
    }


    public class StockLogic : BaseLogic<StockModel, StockFilterModel, Stock, IStockRepository, IStockMapping, IStockService>, IStockLogic
    {
        #region Ioc
        private readonly IBillMapping _IBillMapping;
        private readonly IBillRepository _IBillRepository;

        private readonly IStockWireropeKeepRepository _IStockWireropeKeepRepository;
        private readonly IStockWireropeKeepMapping _IStockWireropeKeepMapping;

        private readonly IBillDetailRepository _IBillDetailRepository;

        public StockLogic(ICurrentUser currentUser, IStockRepository repository, IStockMapping mapping, IStockService service,
            IBillMapping __IBillMapping,
            IBillRepository __IBillRepository,
            IStockWireropeKeepRepository __IStockWireropeKeepRepository,
            IStockWireropeKeepMapping __IStockWireropeKeepMapping,
            IBillDetailRepository __IBillDetailRepository)
            : base(currentUser, repository, mapping, service)
        {
            _IBillRepository = __IBillRepository;
            _IBillMapping = __IBillMapping;

            _IBillDetailRepository = __IBillDetailRepository;

            _IStockWireropeKeepRepository = __IStockWireropeKeepRepository;
            _IStockWireropeKeepMapping = __IStockWireropeKeepMapping;
        }
        #endregion

        #region GetByID
        public override StockModel GetByID(int id)
        {
            var info = GetModel(new StockFilterModel { _StockID = id });

            var where2 = Expressionable.Create<BillDetail>();
            where2 = where2.And(x => x.Code == info.Code);
            var detailInfo = _IBillDetailRepository.GetSearch(where2);

            var billIDs = detailInfo.Select(x => x.BillID).Distinct().ToList();

            var where = Expressionable.Create<Bill>();
            where = where.And(x => billIDs.Contains(x.BillID));
            info.BillModel = _IBillMapping.Entity2Model(_IBillRepository.GetSearch(where));


            var where3 = Expressionable.Create<StockWireropeKeep>();
            where3 = where3.And(x => x.StockID == info.StockID);
            info.StockWireropeKeepModelList = _IStockWireropeKeepMapping.Entity2Model(_IStockWireropeKeepRepository.GetSearch(where3).ToList());

            return info;
        }
        #endregion

        #region Update
        public override int Update(StockModel model)
        {
            if (model.KeepDay.HasValue)
            {
                model.NextKeepDate = DateTime.Now.AddDays(model.KeepDay.Value);
            }

            return _repository.Update(_mapping.Model2Entity(model));
        }
        #endregion

        #region GetSearch
        public override List<StockModel> GetSearch(StockFilterModel filterModel, string orderBy = null, int? top = null)
        {
            var list = base.GetSearch(filterModel, orderBy, top);

            var billIDs = list.Select(x => x.BillID).ToList();

            var where = Expressionable.Create<Bill>();
            where = where.And(x => billIDs.Contains(x.BillID));
            var bills = _IBillMapping.Entity2Model(_IBillRepository.GetSearch(where));

            foreach (var item in list)
            {
                //这里的BillModel被定义成了一个List，问过颖丹了，因为在库存详情查看时，如果要看已出库的绳子信息的话，那么单据就会是多条数据
                //但因为前端已经有地方在使用，而且使用的不少，所以就不改了
                item.BillModel = bills.Where(x => x.BillID == item.BillID).ToList();
            }

            return list;
        }
        #endregion

        #region GetPage
        public override PageModel<StockModel> GetPage(StockFilterModel filterModel, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var pageData = base.GetPage(filterModel, pageIndex, pageSize, orderBy);
            var items = pageData.Items;

            var billIDs = items.Select(x => x.BillID).ToList();

            var where = Expressionable.Create<Bill>();
            where = where.And(x => billIDs.Contains(x.BillID));
            var bills = _IBillMapping.Entity2Model(_IBillRepository.GetSearch(where));

            foreach (var item in items)
            {
                //这里的BillModel被定义成了一个List，问过颖丹了，因为在库存详情查看时，如果要看已出库的绳子信息的话，那么单据就会是多条数据
                //但因为前端已经有地方在使用，而且使用的不少，所以就不改了
                item.BillModel = bills.Where(x => x.BillID == item.BillID).ToList();
            }

            pageData.Items = items;
            return pageData;
        } 
        #endregion
    }
}
